<template>
  <div class="dict-item-box">
    <el-form class="filter-container" @submit.native.prevent :model="queryParams" ref="queryParams" size="small"
      :inline="true" v-show="showSearch">
      <el-form-item label prop="searchValue">
        <el-input v-model="queryParams.searchValue" placeholder="请输入名称/值/备注查询" clearable style="width: 320px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button size="mini" @click="handleAdd" v-hasPermi="['system:dict:item:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <div class="table-box">
      <el-table v-loading="loading" :data="dataList" :cell-style="{ padding: '6px 0px' }">
        <el-table-column align="left" label="字典项名称" prop="dictLabel" width="160">
          <template slot-scope="scope">
            <span v-if="scope.row.listClass == '' || scope.row.listClass == 'default'">{{ scope.row.dictLabel }}</span>
            <el-tag v-else :type="scope.row.listClass == 'primary' ? '' : scope.row.listClass">{{ scope.row.dictLabel
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="值" align="left" prop="dictValue" width="160" :show-overflow-tooltip="true" />
        <el-table-column label="排序" align="center" prop="dictSort" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" active-value="1" inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
        <el-table-column label="操作" align="center" width="150px" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="small" @click="handleUpdate(scope.row)" v-hasPermi="['system:dict:item:edit']">编辑</el-button>
            <el-button size="small" @click="handleDelete(scope.row)"
              v-hasPermi="['system:dict:item:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 右侧编辑/删除 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body
      @close="handleCancle">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典编码">
          <el-input :disabled="true" v-model="form.dictType" placeholder="请输入字典编码" />
        </el-form-item>
        <el-form-item label="名称" prop="dictLabel">
          <el-input v-model="form.dictLabel" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="值" prop="dictValue">
          <el-input v-model="form.dictValue" placeholder="请输入值" />
        </el-form-item>
        <el-form-item label="回显样式" prop="listClass">
          <el-select v-model="form.listClass">
            <el-option v-for="item in listClassOptions" :key="item.value" :label="item.label + '(' + item.value + ')'"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="dictSort">
          <el-input-number v-model="form.dictSort" controls-position="right" :min="0" />
        </el-form-item>

        <!-- <el-form-item label="排序" prop="dictSort">
          <el-input-number v-model="form.dictSort" controls-position="right"></el-input-number>
        </el-form-item> -->

        <el-form-item label="状态" prop="status">
          <el-switch v-model="form.status" style="margin-top: 0" :active-value="1" :inactive-value="0" active-text="有效"
            inactive-text="禁用" v-hasPermi="['system:dict:item:change']" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" size="mini">取消</el-button>
        <el-button type="primary" size="mini" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listData,
  getData,
  delData,
  addData,
  updateData,
} from "@/api/system/dict/data";
export default {
  name: "Data",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      dataList: [],
      // 默认字典类型
      defaultDictType: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数据标签回显样式
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: undefined,
        dictType: undefined,

        searchKey: "",
      },
      // 数据标签回显样式
      listClassOptions: [
        {
          value: "default",
          label: "默认"
        },
        {
          value: "primary",
          label: "主要"
        },
        {
          value: "success",
          label: "成功"
        },
        {
          value: "info",
          label: "信息"
        },
        {
          value: "warning",
          label: "警告"
        },
        {
          value: "danger",
          label: "危险"
        }
      ],
      // 表单参数
      form: {
        status: 1,
        dictSort: 0,
      },
      // 表单校验
      rules: {
        dictLabel: [
          {
            required: true,
            message: "名称不能为空",
            trigger: "blur",
          },
        ],
        dictValue: [
          {
            required: true,
            message: "值不能为空",
            trigger: "blur",
          },
        ],
        dictSort: [
          {
            required: true,
            message: "顺序不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() { },
  methods: {
    init(dict) {
      console.log("--->>init tag0-1", dict);
      this.queryParams.dictType = dict.dictType;
      this.queryParams.searchKey = "";
      this.handleQuery();
    },
    getList() {
      this.loading = true;
      listData(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //编辑
    handleUpdate(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.form.status = Number(row.status);
      this.open = true;
      this.title = "编辑";
      console.log("--->>tag-1", this.form);
    },
    cancel() {
      this.closeDialog();
    },
    handleCancle() {
      this.closeDialog();
    },
    submitForm() {
      console.log("--->> submit ", this.form);
      if (this.form.dictCode) {
        //编辑
        console.log("------>> tag1 ", this.form);
        updateData(this.form).then((response) => {
          this.closeDialog();
          this.getList();
        });
      } else {
        //新增
        console.log("------>> tag2 ", this.form);
        addData(this.form).then((response) => {
          this.closeDialog();
          this.getList();
        });
      }
    },

    //删除item
    handleDelete(row) {
      this.$confirm(`确定进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log("------>> row=", row);
          this.dataListLoading = true;
          delData(row.dictCode)
            .then((response) => {
              this.getList();
            })
            .catch((e) => {
              this.dataListLoading = false;
            });
        })
        .catch(() => {
          this.dataListLoading = false;
        });
    },

    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      //   this.form.dictLabel = "";
      //   this.form.dictValue = "";
      //   this.form.dictType = "";
      //   this.form.remark = "";

      this.form.dictType = this.queryParams.dictType;
      this.form.status = 1;
      this.form.dictSort = 0;
      this.title = "新增";
      this.open = true;
      console.log("--->>tag- form=", this.form);
    },
    // 任务状态编辑
    handleStatusChange(row) {
      updateData(row).then((response) => {
        this.$message.success("操作成功");
      });
    },
    addOrUpdateDictItem(dictItem) {
      this.dictItemEditFormVisible = true;
      dictItem = dictItem ?? { dictType: this.queryParams.dictType };
      this.$nextTick(() => {
        this.$refs.dictItemEditForm.init(dictItem);
      });
    },

    changeDictItemStatus(id) {
      $gc.dictService
        .changeDictItemStatus(id)
        .then((data) => {
          this.$message.success("操作成功");
          this.getDataList();
        })
        .catch((e) => {
          console.error(e);
        });
    },

    closeDialog() {
      console.log("--->>tag0 弹窗关闭");
      this.form = {};
      this.$refs.form.clearValidate();
      this.open = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.dict-item-box {
  .dict-btn-list {
    padding: 16px 16px 6px;
  }

  .el-table {
    .cell {
      padding: 0 14px;
    }
  }
}
</style>
